// 测试基本路由

const express = require('express');

const port = 8080;
const app = express();

//使用url编码的有效负载解析传入请求
app.use(express.urlencoded({ extended: true }));

// app.METHOD( PATH , HANDLER )
app.get('/', (req, resp) => {
    console.log('url: ', req.url);
    console.log('query: ', req.query);
    console.log('params:', req.params);
    console.log('body: ', req.body);
    // 确定响应状态代码、设置响应头、确定响应正文
    resp.send(`<h1>${new Date().toLocaleString()}</h1>`);
});

app.get('/:name/:gender/:age', (req, resp) => {
    console.log('url: ', req.url);
    console.log('query: ', req.query);
    console.log('params:', req.params);
    console.log('body: ', req.body);
    // 确定响应状态代码、设置响应头、确定响应正文
    resp.send(`<h1>${new Date().toLocaleString()}</h1>`);
});

app.get('/json', (req, resp) => {
    /*let student = {id: 1001,name: '张三丰',gender: '靓仔',age: 108}*/
    let student = Object.create(null);
    Object.defineProperty(student, 'id', { value: 2002, enumerable: true });
    student.name = '张三丰';
    student.gender = '靓仔';
    student.age = 108;
    console.log(student);
    resp.send(student); //输出类型type=document;
});

app.listen(port, () => {
    console.log('服务器已经启动');
});